import Map from 'ol/Map'
import View from 'ol/View'
import TileLayer from 'ol/layer/Tile'

import GeoJSON from 'ol/format/GeoJSON'
import { TileWMS, OSM, ImageArcGISRest } from 'ol/source'
import TileGrid from 'ol/tilegrid/TileGrid'
import TileImage from 'ol/source/TileImage'
import VectorSource from 'ol/source/Vector'
import { XYZ  } from 'ol/source.js'
// import {ImageArcGISRest, OSM} from 'ol/source.js';
// import {Image as ImageLayer, Tile as TileLayer} from 'ol/layer.js';
import {  Image as ImageLayer } from 'ol/layer.js'
import VectorImageLayer from 'ol/layer/VectorImage.js'
import { Circle, Fill, Icon, Stroke, Style, Text } from 'ol/style.js'
import { fromLonLat, toLonLat, transform, transformExtent } from 'ol/proj'
import { get } from 'ol/proj'
import * as maputils from './maputils'
import * as onemap from '../onemap'
import config from '../../config/index'


let map
//地图
const initMap = (options) => {
  map = new Map({
    target: options.map,
    view: new View({
      center: transform([113.304788, 28.013391], 'EPSG:4326', 'EPSG:3857'),
      zoom: 4,
    })
  })
  window.map = map
  MapManager.getInstance().loadMapSource( 'gaode-vec')
  return map
}



// 创建一个地图管理层
class MapManager {
  constructor() {
    this.currentLayer = null // 当前加载的图层
    this.instance = null
  }

  static getInstance() {
    if (!this.instance) {
      this.instance = new MapManager()
    }
    return this.instance
  }

  // 加载地图源
  loadMapSource(type) {
    let layer
    switch (type) {
      case 'tianditu':
        layer = this.createTiandituLayer()
        break
      case 'tianditu-vec':
        layer = this.createTiandituVecLayer()
        break
      case 'tianditu-mark':
        layer = this.createTiandituLayerMark()
        break
      case 'baidu':
        layer = this.createBaiduLayer()
        break
      case 'gaode-vec':
        layer = this.createGaodeVecLayer()
        break
      case 'gaode-mark':
        layer = this.createGaodeLayerMark()
        break
      case 'gaode':
        layer = this.createGaodeLayer()
        break
      case 'osm':
        layer = this.createOsmLayer()
        break
      case 'arcgis':
        layer = this.createArcgis()
      case 'google':
        layer = this.createGoogle()
        break

      // 可以根据需要添加更多类型的图层加载方法
      default:
        throw new Error('Unsupported map source type')
    }

    if (this.currentLayer) {
      if (Array.isArray(this.currentLayer)) {
        for (const item of this.currentLayer) {
          map.removeLayer(item)
        }
      } else {
        map.removeLayer(this.currentLayer)
      }
    }

    if (Array.isArray(layer)) {
      for (const item of layer) {
        item.setZIndex(-100);
        map.addLayer(item)
      }
    } else {
      layer.setZIndex(-100);
      map.addLayer(layer)
    }
    this.currentLayer = layer
  }
  //天地图地图
  createTiandituVecLayer() {
    const projection = get('EPSG:4326')

    var map_img = new TileLayer({
      id: 'ditu-1',
      source: new XYZ({
        projection: projection,
        url: `http://t{0-7}.tianditu.gov.cn/DataServer?T=vec_c&x={x}&y={y}&l={z}&tk=${config.TDTTK}`,
        crossOrigin: 'anonymous',
        wrapX: false
      })
    })

    var map_cta = new TileLayer({
      id: 'ditu-1',
      source: new XYZ({
        url: `http://t4.tianditu.com/DataServer?T=cva_w&tk=${config.TDTTK}&x={x}&y={y}&l={z}`,
        crossOrigin: 'anonymous'
      })
    })
    return [map_img, map_cta]
  }
  //天地图影像
  createTiandituLayer() {
    const projection = get('EPSG:3857')
    return new TileLayer({
      id: 'ditu-1',
      source: new XYZ({
        projection: projection,
        url: `http://t3.tianditu.com/DataServer?T=img_w&tk=${config.TDTTK}&x={x}&y={y}&l={z}`,
        crossOrigin: 'anonymous',
        wrapX: false
      })
    })
  }

  //天地图影像标注
  createTiandituLayerMark() {
    const projection = get('EPSG:3857')
    var a = new TileLayer({
      id: 'ditu-1',
      source: new XYZ({
        projection: projection,
        url: `http://t3.tianditu.com/DataServer?T=img_w&tk=${config.TDTTK}&x={x}&y={y}&l={z}`,
        crossOrigin: 'anonymous',
        wrapX: false
      })
    })

    var map_cta = new TileLayer({
      id: 'ditu-1',
      source: new XYZ({
        url: `http://t4.tianditu.com/DataServer?T=cva_w&tk=${config.TDTTK}&x={x}&y={y}&l={z}`,
        crossOrigin: 'anonymous'
      })
    })

    return [a, map_cta]
  }

  // 创建高德图层
  createGaodeVecLayer() {
    return new TileLayer({
      id: 'ditu-1',
      source: new XYZ({
        url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
        crossOrigin: 'anonymous',
        wrapX: false
      })
    })
  }
  createGaodeLayer() {
    return new TileLayer({
      id: 'ditu-1',
      source: new XYZ({
        url: 'https://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}',
        crossOrigin: 'anonymous',
        wrapX: false
      })
    })
  }
  createGaodeLayerMark() {
    var a = new TileLayer({
      id: 'ditu-1',
      source: new XYZ({
        url: 'https://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}',
        crossOrigin: 'anonymous',
        wrapX: false
      })
    })
    var b = new TileLayer({
      id: 'ditu-1',
      source: new XYZ({
        url: 'http://webst02.is.autonavi.com/appmaptile?style=8&x={x}&y={y}&z={z}',
        crossOrigin: 'anonymous',
        wrapX: false
      })
    })
    return [a, b]
  }
  //

  // 创建百度图层
  createBaiduLayer() {

    // 创建百度地图的source
const baiduSource = new TileImage({
  projection: 'EPSG:3857',
  crossOrigin: 'anonymous',
  tileGrid: new TileGrid({
    origin: [0, 0],
    resolutions: Array.from({ length: 19 }, (_, i) => Math.pow(2, 18 - i))
  }),
  tileUrlFunction: function (tileCoord) {
    if (!tileCoord) return '';
    let z = tileCoord[0];
    let x = tileCoord[1];
    let y = -tileCoord[2] - 1; 
    // 对编号xy处理
    let baiduX = x < 0? 'M' + -x : x;
    let baiduY = y < 0? 'M' + -y : y;
    return 'http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=' +
      baiduX + '&y=' + baiduY + '&z=' + z + '&styles=pl&udt=20151021&scaler=1&p=1';
  }
});
    return new TileLayer({
      id: 'ditu-1',
      source: baiduSource
    })
  }

  // 创建OSM图层
  createOsmLayer() {
    return new OSM()
  }

  createArcgis() {
    return new TileLayer({
      id: 'ditu-1',
      source: new XYZ({
        url:
          'http://server.arcgisonline.com/ArcGIS/rest/services/' +
          'World_Topo_Map/MapServer/tile/{z}/{y}/{x}',
        crossOrigin: 'anonymous'
      })
    })
  }

  createGoogle() {
    return new TileLayer({
      id: 'ditu-1',
      source: new XYZ({
        url: 'http://0414.gggis.com/maps/vt?lyrs=s&x={x}&y={y}&z={z}',
        crossOrigin: 'anonymous',
        wrapX: false
      })
    })
  }

}



export { initMap, loadAllLayers, MapManager, loadFilterLayer,loadEarthQuakeYXC, delEarthQuakeYXC,initOtherMap }
